
package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-9-9
* Time: 下午12:56
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("serial")
@TableBind(tableName="license_log",pkName = "log_id")
public class LicenseLog extends BasicModel<LicenseLog> {
    public static final LicenseLog dao = new LicenseLog();
    public static final String SELECT_ALL = "`log_id`,`operation_time`,`type`,`from_object`,`to_object`,`num`,`last_os`,`remark`,`status` ";

    public enum Type{
        /*操作成功,返回*/
        CREATE(0, "创建"),
        TRANSFER(1, "转让"),
        RECHARGE(2, "充值"),
        GETRETURN(3, "取回"),
        USE(4, "使用"),
        RETURN(5, "退回");

        private Integer code;
        private String value;

        public Integer getCode() {
            return code;
        }

        public String getValue() {
            return value;
        }

        private Type(Integer code, String value) {
            this.code = code;
            this.value = value;
        }
    }

    public LicenseLog findLog(LicenseLog log){
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("SELECT * FROM " + getTableName());
        sqlExceptSelectBuffer.append(" where 1=1 ");
        if (log != null) {
            String[] attrNames = log.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = log.get(attrName);
                if (attrValue != null) {

                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `operation_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `operation_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else{
                       /* int index = attrName.indexOf(".");
                        if (index > 0){
                            sqlExceptSelectBuffer.append(" AND `"+attrName.substring(0,index)+"`.`" + attrName.substring(index+1) + "`=?");
                        }else {*/
                        sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
//                        }
                    }

                    paras.add(attrValue);
                }
            }
            sqlExceptSelectBuffer.append(" ORDER BY `operation_time` DESC ");
        }
        return findFirst(sqlExceptSelectBuffer.toString(),paras.toArray());
    }

    public List<LicenseLog> findDeviceFailLog(long userId ,long deviceId , long os){
        List<Object> paras = new ArrayList<>();
        StringBuffer sb = new StringBuffer("SELECT * FROM " + getTableName());
        sb.append(" where STATUS = 0 ");
        sb.append(" and ((TYPE = 2 AND from_object = ? AND to_object = ? ) OR (TYPE = 3 AND to_object = ? AND from_object = ? )) ");
        sb.append(" AND last_os < ? ");
        paras.add(userId);
        paras.add(deviceId);
        paras.add(userId);
        paras.add(deviceId);
        paras.add(os);

        return find(sb.toString(),paras.toArray());
    }

    @Override
    public Page<LicenseLog> paginate(Integer pageNumber, Integer pageSize, String column, LicenseLog log) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName());
//        sqlExceptSelectBuffer.append(other);
        sqlExceptSelectBuffer.append(" where 1=1 ");
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (log != null) {
            String[] attrNames = log.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = log.get(attrName);
                if (attrValue != null) {

                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `operation_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `operation_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else{
                       /* int index = attrName.indexOf(".");
                        if (index > 0){
                            sqlExceptSelectBuffer.append(" AND `"+attrName.substring(0,index)+"`.`" + attrName.substring(index+1) + "`=?");
                        }else {*/
                            sqlExceptSelectBuffer.append(" AND `" + attrName + "`=?");
//                        }
                    }
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }
    
        public LicenseLog setLogId (Long logId){
        this.set("log_id",logId);
        return this;
    }

    public Long getLogId (){
        return this. get("log_id");
    }

        public LicenseLog setOperationTime (Date operationTime){
        this.set("operation_time",operationTime);
        return this;
    }

    public Date getOperationTime (){
        return this.getDate("operation_time");
    }

        public LicenseLog setType (Integer type){
        this.set("type",type);
        return this;
    }

    public Integer getType (){
        return this. get("type");
    }

        public LicenseLog setFromObject (Long fromObject){
        this.set("from_object",fromObject);
        return this;
    }

    public Long getFromObject (){
        return this. get("from_object");
    }

        public LicenseLog setToObject (Long toObject){
        this.set("to_object",toObject);
        return this;
    }

    public Long getToObject (){
        return this. get("to_object");
    }

        public LicenseLog setNum (Long num){
        this.set("num",num);
        return this;
    }

    public Long getNum (){
        return this. get("num");
    }

        public LicenseLog setLastOs (Long lastOs){
        this.set("last_os",lastOs);
        return this;
    }

    public Long getLastOs (){
        return this.getLong("last_os");
    }

        public LicenseLog setRemark (String remark){
        this.set("remark",remark);
        return this;
    }

    public String getRemark (){
        return this.getStr("remark");
    }

        public LicenseLog setStatus (Integer status){
        this.set("status",status);
        return this;
    }

    public Integer getStatus (){
        return this. get("status");
    }
}
